//
//  SplashVC.m
//  CJModileAdDemo
//
//  Created by CJ on 2022/3/7.
//

#import "SplashVC.h"
#import "CustomShowView.h"
#import <BUAdSDK/BUAdSDK.h>

@interface SplashVC ()<BUMSplashAdDelegate>

@property (nonatomic, strong) BUSplashAd *splashAd;

@end

@implementation SplashVC

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)loadAdData {
    BUSplashAd *splashAd = [[BUSplashAd alloc] initWithSlotID:@"103600746" adSize:[UIScreen mainScreen].bounds.size];
    splashAd.supportCardView = YES;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
    splashAd.mediation.customBottomView = view;
    view.backgroundColor = [UIColor redColor];
//    splashAd.supportZoomOutView = YES;
    
    // 不支持中途更改代理，中途更改代理会导致接收不到广告相关回调，如若存在中途更改代理场景，需自行处理相关逻辑，确保广告相关回调正常执行。
    splashAd.delegate = self;
    splashAd.tolerateTimeout = 3;
    /***
    广告加载成功的时候，会立即渲染WKWebView。
    如果想预加载的话，建议一次最多预加载三个广告，如果超过3个会很大概率导致WKWebview渲染失败。
    */
    self.splashAd = splashAd;
    [self.splashAd loadAdData];
}

- (void)setupUI {
    self.title = @"SplashAd";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        NSLog(@"事件:%s", __FUNCTION__);
        [self loadAdData];
        return;
    }
    [self.splashAd showSplashViewInRootViewController:self];
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak SplashVC *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

#pragma mark ATSplashDelegate
- (void)splashAdLoadSuccess:(BUSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
    [splashAd showSplashViewInRootViewController:self];
}

- (void)splashAdWillShow:(BUSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
}

// 穿山甲gm不走··，只会走will on show
- (void)splashAdDidShow:(BUSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)splashAdLoadFail:(nonnull BUSplashAd *)splashAd error:(BUAdError * _Nullable)error {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)splashAdRenderSuccess:(nonnull BUSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)splashAdDidClick:(nonnull BUSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)splashAdDidClose:(nonnull BUSplashAd *)splashAd closeType:(BUSplashAdCloseType)closeType {
    NSLog(@"事件:%s", __FUNCTION__);
}

@end
